package com.bbx.taxi.client.Service;

import java.util.List;

import com.bbx.androidapi.util.SystemUtil;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.SDK;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.TokenNet;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.login.LoginActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Message.MyMesssage;
import com.bbx.taxi.client.Util.AppStarted;
import com.bbx.taxi.client.Util.DataCleanManager;

import android.app.ActivityManager;
import android.app.Service;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.text.TextUtils;

public class TokenService extends Service {
	private TokenTask mTokenTask;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		Token token = SharedPreEncryptUtil.getStringEncryptValue(this,
				SharedPreEncryptUtil.TokenAndUid, "");
		if (token != null) {
			onRequestToken(this, token);
		}
	}

	/**
	 * 请求Token
	 */
	public void onRequestToken(Context context, Token token) {
		mTokenTask = new TokenTask(context, token);
		mTokenTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	public class TokenTask extends BaseAsyncTask {

		private Token token;

		public TokenTask(Context context, Token token) {
			super(context, false,0);
			this.token = token;
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(token).getJson();
			return new TokenNet(context, json);
		}

		@Override
		public void onSuccess(int status,Object object) {
			Token mToken = (Token) object;
			if (mToken != null) {
				if (mToken.getAccess_token() != null) {
					token.access_token = mToken.getAccess_token();
					SharedPreEncryptUtil.putStringEncryptValue(context,
							SharedPreEncryptUtil.TokenAndUid, token);
					context.startService(new Intent(context, LoginService.class));
					context.stopService(new Intent(context, TokenService.class));
				} else {
					Failed(context);
				}
			} else {
				Failed(context);
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			if (status == -10105||status == -10108) {
				Failed(context);
			}
		}
	}

	public void Failed(Context context) {
		Value.isUnSDK = true;
		DataCleanManager.cleanDatabases(context);
		DataCleanManager.cleanSharedPreference(context);
		DataCleanManager.cleanFiles(context);
		DataCleanManager.cleanExternalCache(context);
		context.stopService(new Intent(context, LoginService.class));
		context.stopService(new Intent(context, TokenService.class));
		// token过期了
		if (AppStarted.isRunningForeground(context)) {
			ToastUtil.showToast(context, R.string.account_exception);
			Intent intent = new Intent(context, LoginActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		}
	}

	// 获取程序自身包名
	public static String getAppPackageName(Context context) {
		try {
			String pkName = context.getPackageName();
			return pkName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
